//
//  HTSysRecDataShow.m
//  HTAppBeauty
//
//  Created by standychen on 14-11-19.
//  Copyright (c) 2014年 HT. All rights reserved.
//

#import "HTSysRecDataShow.h"
#import "HTFrameConvert.h"

//处理接收数据的显示到界面的处理
@implementation HTSysRecDataShow



//获得处理数据的连接字符串,标准方法直接相连接，每个字段及值占用一行
+(NSString *)hmRecFromRecTitle:(NSDictionary*)titleDict andRecDataOne:(NSDictionary *)titleDataDict{
    NSMutableString *strconn=[[NSMutableString alloc] init];
    
    int recheadcnum=[[titleDict objectForKey:@"cnum"] intValue];
    
    NSString *strkey;
    NSString *strdatakey;
    NSString *strtype;
    for (int i=0; i<recheadcnum; i++) {
        if([strconn isEqual:@""]){// 如果有值内容换行
        }
        else{
            [strconn appendString:@"\n"];
        }
        strkey=[[NSString alloc] initWithFormat:@"cv%i",i+1];//可见性
        if([[titleDict objectForKey:strkey] isEqual:@"0"]){//不可见
            
        }
        else{
            strkey=[[NSString alloc] initWithFormat:@"ct%i",i+1];
            [strconn appendString:[titleDict objectForKey:strkey]];//标题
            [strconn appendString:@": "];//等于
            
            
            strkey=[[NSString alloc] initWithFormat:@"cd%i",i+1];
            strtype=[titleDict objectForKey:strkey];//数据类型
            
            
            strkey=[[NSString alloc] initWithFormat:@"cn%i",i+1];
            strdatakey=[titleDict objectForKey:strkey];
            [strconn appendString:[self hmConvertRecData:strtype andValue:[titleDataDict objectForKey:strdatakey]]];//数据内容
        }
    }
    
    return [[NSString alloc] initWithString:strconn];
}


//获得一个DataTable全部数据 处理数据的连接字符串,标准方法直接相连接，每个字段及值占用一行，每行占用一节，一节多一个换行符
+(NSString *)hmRecFromRecTitle:(NSDictionary*)titleDict andRecDataAll:(NSMutableArray *)titleDataArray{
    NSMutableString *strconn=[[NSMutableString alloc] init];
    
    for (int i=0; i<[titleDataArray count]; i++) {
        if([strconn isEqual:@""]){// 如果有值内容换两行
        }
        else{
            [strconn appendString:@"\n\n"];
        }
        NSDictionary *oneDataDict=[titleDataArray objectAtIndex:i];
        
        [strconn appendString:[self hmRecFromRecTitle:titleDict andRecDataOne:oneDataDict]];
        
    }
    
    return [[NSString alloc] initWithString:strconn];
}


//获得传入数据根据类型转换为字符串
+(NSString *)hmConvertRecData:(NSString *)strType andValue:(id)idvalue{
    NSString *retStr=[[NSString alloc] init];
    
    if ((NSNull *)idvalue == [NSNull null]) {//如果返回的null
        return retStr;
    }
    BOOL procflag=FALSE;
    if ([strType length]==2) {//处理浮点型数据 f1,f2,..f5
        NSRange range=NSMakeRange(0, 1);
        if ([[strType substringWithRange:range] isEqualToString:@"f"]) {
            NSRange range=NSMakeRange(1, 1);
            //NSString *strtemp=idvalue;
            //double dv=[strtemp doubleValue];
            id dv=idvalue;
            switch ([[strType substringWithRange:range] intValue]) {
                case 0:
                    retStr=[[NSString alloc]initWithFormat:@"%.0f",dv];
                    break;
                case 1:
                    retStr=[[NSString alloc]initWithFormat:@"%.1f",dv];
                    break;
                case 2:
                    retStr=[[NSString alloc]initWithFormat:@"%.2f",dv];
                    break;
                case 3:
                    retStr=[[NSString alloc]initWithFormat:@"%.3f",dv];
                    break;
                case 4:
                    retStr=[[NSString alloc]initWithFormat:@"%.4f",dv];
                    break;
                case 5:
                    retStr=[[NSString alloc]initWithFormat:@"%.5f",dv];
                    break;
                default:
                    retStr=[idvalue stringValue];
                    break;
            }
            procflag=TRUE;
        }
    }
    if (procflag) {//如果已处理过，不执行下列转换判定
        
    }else if ([strType isEqualToString:@"d"]) {
        retStr=[HTFrameConvert convertdateStrWithJsonStr:idvalue andWithFormat:@"yyyy-MM-dd"];
    }
    else if ([strType isEqualToString:@"t"]){
        retStr=[HTFrameConvert convertdateStrWithJsonStr:idvalue andWithFormat:@"HH:mm:ss"];
    }
    else if ([strType isEqualToString:@"dt"]){
        retStr=[HTFrameConvert convertdateStrWithJsonStr:idvalue andWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    }
    else if ([strType isEqualToString:@"ds"]){
        retStr=[HTFrameConvert convertdateStrWithJsonStr:idvalue andWithFormat:@"MM/dd"];
    }
    else if ([strType isEqualToString:@"ts"]){
        retStr=[HTFrameConvert convertdateStrWithJsonStr:idvalue andWithFormat:@"HH:mm"];
    }
    else if ([strType isEqualToString:@"s"]){
        retStr=idvalue;//字符串不动
    }
    else{
        retStr=[idvalue stringValue];
    }
    
    
    return retStr;
}


@end
